const path = require('path')
const posthtml = require('posthtml')
const { readImgSrcToBase64 } = require('./readImgSrcToBase64.js')
const trImg = (absPath) => {
    return (tree) => {
        tree.match({ tag: "img" }, node => {
            const imgPath = node.attrs.src
            const absImgPath = path.resolve(absPath, imgPath)
            const base64 = readImgSrcToBase64(absImgPath)
            return {
                tag: 'img',
                attrs: { src: base64 }
            }
        });
    }
}
const transFormHtmlImgSrcToBase64 = (html, absPath) => {
    const trImgFunc = trImg(absPath)
    return posthtml().use(trImgFunc).process(html, { sync: true }).html
}
module.exports = {
    transFormHtmlImgSrcToBase64
}